﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChiTietPhieuGiaoDAO
    {
        public static string CreateChiTietPhieuGiaoId()
        {
            DataTable dataTable = DataProvider.ExecuteReader("sp_CreatePhieuDatID", null);
            string ID = null;

            if (dataTable.Rows.Count > 0)
            {
                DataRow dataRow = dataTable.Rows[0];
                ID = dataRow[0].ToString();
            }

            return ID;
        }

        public static DataTable SelectChiTietPhieuGiaoAll()
        {
            DataTable dataTable = DataProvider.ExecuteReader("sp_SelectChiTietPhieuGiaoAll");
            return dataTable;
        }

        public static ChiTietPhieuGiaoDTO SelectChiTietPhieuGiaoById(string maChiTietPhieuGiao)
        {
            List<SqlParameter> sqlParamas = new List<SqlParameter>();
            sqlParamas.Add(new SqlParameter("@maChiTietPG", maChiTietPhieuGiao));
            DataTable dataTable = DataProvider.ExecuteReader("sp_SelectChiTietPhieuGiaoById", sqlParamas);

            ChiTietPhieuGiaoDTO ctPhieuGiaoDTO = new ChiTietPhieuGiaoDTO();
            if (dataTable.Rows.Count > 0)
            {
                DataRow dataRow = dataTable.Rows[0];
                ctPhieuGiaoDTO.MaChiTietPG = maChiTietPhieuGiao;
                ctPhieuGiaoDTO.Ma_PG = dataRow["Ma_PG"].ToString();
                ctPhieuGiaoDTO.Ma_Mon = dataRow["Ma_Mon"].ToString();
                ctPhieuGiaoDTO.SoLuong = int.Parse(dataRow["SoLuong"].ToString());
                ctPhieuGiaoDTO.DonGia = float.Parse(dataRow["DonGia"].ToString());
                ctPhieuGiaoDTO.SoHop = int.Parse(dataRow["SoHop"].ToString());

            }
            else
            {
                ctPhieuGiaoDTO = null;
            }
            return ctPhieuGiaoDTO;
        }

        public static bool InsertChiTietPhieuGiao(ChiTietPhieuGiaoDTO ctPhieuGiaoDTO)
        {
            List<SqlParameter> sqlParamas = new List<SqlParameter>();
            sqlParamas.Add(new SqlParameter("@maChiTietPG", ctPhieuGiaoDTO.MaChiTietPG));
            sqlParamas.Add(new SqlParameter("@ma_PG", ctPhieuGiaoDTO.Ma_PG));
            sqlParamas.Add(new SqlParameter("@ma_Mon", ctPhieuGiaoDTO.Ma_Mon));
            sqlParamas.Add(new SqlParameter("@soLuong", ctPhieuGiaoDTO.SoLuong));
            sqlParamas.Add(new SqlParameter("@donGia", ctPhieuGiaoDTO.DonGia));
            sqlParamas.Add(new SqlParameter("@soHop", ctPhieuGiaoDTO.SoHop));
            
            return DataProvider.ExecuteNoneQuery("sp_InsertChiTietPhieuGiao", sqlParamas);        
        }

        public static bool UpdateChiTietPhieuGiaoById(ChiTietPhieuGiaoDTO ctPhieuGiaoDTO)
        {
            List<SqlParameter> sqlParamas = new List<SqlParameter>();
            sqlParamas.Add(new SqlParameter("@maChiTietPG", ctPhieuGiaoDTO.MaChiTietPG));
            sqlParamas.Add(new SqlParameter("@ma_PG", ctPhieuGiaoDTO.Ma_PG));
            sqlParamas.Add(new SqlParameter("@ma_Mon", ctPhieuGiaoDTO.Ma_Mon));
            sqlParamas.Add(new SqlParameter("@soLuong", ctPhieuGiaoDTO.SoLuong));
            sqlParamas.Add(new SqlParameter("@donGia", ctPhieuGiaoDTO.DonGia));
            sqlParamas.Add(new SqlParameter("@soHop", ctPhieuGiaoDTO.SoHop));

            return DataProvider.ExecuteNoneQuery("sp_UpdateChiTietPhieuGiaoById", sqlParamas);                
        }

        public static bool DeleteChiTietPhieuGiaoById(string maChiTietPhieuGiao)
        {
            List<SqlParameter> sqlParamas = new List<SqlParameter>();
            sqlParamas.Add(new SqlParameter("@maChiTietPG", maChiTietPhieuGiao));
            return DataProvider.ExecuteNoneQuery("sp_DeleteChiTietPhieuGiaoById", sqlParamas);        
        
        }
    }
}
